package com.kmproject.haifu.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.di.component.DaggerPersonalHpStoreComponent;
import com.kmproject.haifu.di.module.PersonalHpStoreModule;
import com.kmproject.haifu.mvp.contract.PersonalHpStoreContract;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoreMainBean;
import com.kmproject.haifu.mvp.presenter.PersonalHpStorePresenter;
import com.kmproject.haifu.mvp.ui.widget.ExpandableTextView;

import java.util.Objects;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 *
 */
public class PersonalHpStoreFragment extends BaseFragment<PersonalHpStorePresenter> implements PersonalHpStoreContract.View {
    @BindView(R.id.mystore_story_dpmc)
    TextView mystoreStoryDpmc;
    @BindView(R.id.mystore_story_dplx)
    TextView mystoreStoryDplx;
    @BindView(R.id.mystore_story_lxdh)
    TextView mystoreStoryLxdh;
    @BindView(R.id.mystore_story_rzlx)
    TextView mystoreStoryRzlx;
    @BindView(R.id.mystore_story_xxdz)
    TextView mystoreStoryXxdz;
    @BindView(R.id.mystore_story_kdsj)
    TextView mystoreStoryKdsj;
    @BindView(R.id.mystore_story_yyzz_img)
    ImageView mystoreStoryYyzzImg;
    @BindView(R.id.mystore_story_jj_ExpandableTextView7)
    ExpandableTextView mystoreStoryJjExpandableTextView7;

    public static PersonalHpStoreFragment newInstance() {
        PersonalHpStoreFragment fragment = new PersonalHpStoreFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerPersonalHpStoreComponent
                .builder()
                .appComponent(appComponent)
                .personalHpStoreModule(new PersonalHpStoreModule(this))//请将PersonalHpStoreModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mystore_story, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {
        String storeId = (String) data;
        mPresenter.getStoreInfo(storeId);
    }


    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
    }

    @Override
    public void showStoreInfo(MyStoreMainBean myStoreMainBean) {
        if (myStoreMainBean != null) {
            mystoreStoryDpmc.setText(myStoreMainBean.getStoreName());
            mystoreStoryDplx.setText(myStoreMainBean.getStoreTypeName());
            mystoreStoryLxdh.setText(myStoreMainBean.getStoreMobile());
            mystoreStoryRzlx.setText((myStoreMainBean.getPassport() != null && !TextUtils.isEmpty(myStoreMainBean.getPassport()) ? "护照、" : "") +
                    ((myStoreMainBean.getIdCardFront() != null && !TextUtils.isEmpty(myStoreMainBean.getIdCardFront()) && myStoreMainBean.getIdCardReverseSide() != null
                            && !TextUtils.isEmpty(myStoreMainBean.getIdCardReverseSide()) ? "身份证、" : "") + "营业执照"));
            mystoreStoryXxdz.setText(myStoreMainBean.getAddressDetail());
            mystoreStoryKdsj.setText(myStoreMainBean.getCreateTime());
            ((BaseActivity) Objects.requireNonNull(getActivity())).getMglideImage().loadImage(getContext(), ImageConfigImpl.builder().url(myStoreMainBean.getBusinessLicense()).imageRadius(5)
                    .imageView(mystoreStoryYyzzImg)
                    .build());
            mystoreStoryJjExpandableTextView7.setOriginalText(myStoreMainBean.getStoreDetail());
        }
    }
}